﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace CheemsControl.Controls
{
    /// <summary>
    /// 文件拖放控件
    /// </summary>
    public class FileDragDropBorder : Border
    {
        static FileDragDropBorder()
        {
            // 设置默认样式
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FileDragDropBorder),
                new FrameworkPropertyMetadata(typeof(FileDragDropBorder)));
        }

        public FileDragDropBorder()
        {
            AllowDrop = true;
            DragEnter += OnDragEnter;
            Drop += OnDrop;

            // 确保 Source 的变化通知绑定
            Source = new ObservableCollection<string>();
            BindingOperations.SetBinding(this, SourceProperty, new Binding(nameof(Source))
            {
                Source = this,
                Mode = BindingMode.TwoWay
            });
        }


        /// <summary>
        /// 拖放的所有文件路径
        /// </summary>
        public ObservableCollection<string> Source
        {
            get { return (ObservableCollection<string>)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register(
                "Source",
                typeof(ObservableCollection<string>),
                typeof(FileDragDropBorder),
                new FrameworkPropertyMetadata(
                    null,
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault // 默认支持双向绑定
                )
            );

        /// <summary>
        /// 拖拽进入事件处理
        /// </summary>
        private void OnDragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Copy;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }

        /// <summary>
        /// 文件放置事件处理
        /// </summary>
        private void OnDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop);
                Source = new ObservableCollection<string>(paths);
            }
        }

    }
}
